
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_Hans">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>FAQ：数据库和模型 &#8212; Django 3.2.11.dev 文档</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    <link rel="index" title="索引" href="../genindex.html" />
    <link rel="search" title="搜索" href="../search.html" />
    <link rel="next" title="FAQ：管理" href="admin.html" />
    <link rel="prev" title="FAQ：获取帮助" href="help.html" />



 
<script src="../templatebuiltins.js"></script>
<script>
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);</script>

  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../index.html">Django 3.2.11.dev 文档</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../index.html">Home</a>  |
        <a title="Table of contents" href="../contents.html">Table of contents</a>  |
        <a title="Global index" href="../genindex.html">Index</a>  |
        <a title="Module index" href="../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="help.html" title="FAQ：获取帮助">previous</a>
     |
    <a href="index.html" title="Django FAQ" accesskey="U">up</a>
   |
    <a href="admin.html" title="FAQ：管理">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="faq-models">
            
  <div class="section" id="s-faq-databases-and-models">
<span id="faq-databases-and-models"></span><h1>FAQ：数据库和模型<a class="headerlink" href="#faq-databases-and-models" title="永久链接至标题">¶</a></h1>
<div class="section" id="s-how-can-i-see-the-raw-sql-queries-django-is-running">
<span id="s-faq-see-raw-sql-queries"></span><span id="how-can-i-see-the-raw-sql-queries-django-is-running"></span><span id="faq-see-raw-sql-queries"></span><h2>我该如何在Django运行的过程中看到SQL查询？<a class="headerlink" href="#how-can-i-see-the-raw-sql-queries-django-is-running" title="永久链接至标题">¶</a></h2>
<p>首先确保你的 Django <a class="reference internal" href="../ref/settings.html#std:setting-DEBUG"><code class="xref std std-setting docutils literal notranslate"><span class="pre">DEBUG</span></code></a> 选项为 <code class="docutils literal notranslate"><span class="pre">True</span></code>。然后，这样做：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db</span> <span class="kn">import</span> <span class="n">connection</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">connection</span><span class="o">.</span><span class="n">queries</span>
<span class="go">[{&#39;sql&#39;: &#39;SELECT polls_polls.id, polls_polls.question, polls_polls.pub_date FROM polls_polls&#39;,</span>
<span class="go">&#39;time&#39;: &#39;0.002&#39;}]</span>
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">connection.queries</span></code> 只有在 <a class="reference internal" href="../ref/settings.html#std:setting-DEBUG"><code class="xref std std-setting docutils literal notranslate"><span class="pre">DEBUG</span></code></a> 为 <a href="#id1"><span class="problematic" id="id2">``</span></a>True``时才可用. 它是按顺序执行的字典列表，每个字典包括:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>``sql`` -- The raw SQL statement
``time`` -- How long the statement took to execute, in seconds.
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">connection.queries</span></code> 包含了所有的SQL 语句 -- INSERT, UPDATES, SELECT, 等. 每次你的应用访问了数据库，查询都会被记录下来。</p>
<p>如果你在使用 <span class="xref std std-doc">multiple databases1</span>，你可以对 <code class="docutils literal notranslate"><span class="pre">connections</span></code> 字典的每个成员使用相同的接口:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.db</span> <span class="kn">import</span> <span class="n">connections</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">connections</span><span class="p">[</span><span class="s1">&#39;my_db_alias&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">queries</span>
</pre></div>
</div>
<p>如果需要在函数中的任何位置手动清除查询列表，只需要调用 <code class="docutils literal notranslate"><span class="pre">reset_queries()</span></code>，像这样：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.db</span> <span class="kn">import</span> <span class="n">reset_queries</span>
<span class="n">reset_queries</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="section" id="s-can-i-use-django-with-a-pre-existing-database">
<span id="can-i-use-django-with-a-pre-existing-database"></span><h2>我可以让 Django 用一个之前已经存在的数据库吗？<a class="headerlink" href="#can-i-use-django-with-a-pre-existing-database" title="永久链接至标题">¶</a></h2>
<p>是的，可以看这篇文档 <a class="reference internal" href="../howto/legacy-databases.html"><span class="doc">导入已有数据库</span></a>。</p>
</div>
<div class="section" id="s-if-i-make-changes-to-a-model-how-do-i-update-the-database">
<span id="if-i-make-changes-to-a-model-how-do-i-update-the-database"></span><h2>如果我对一个模型做了改动，我该如何去更新数据库？<a class="headerlink" href="#if-i-make-changes-to-a-model-how-do-i-update-the-database" title="永久链接至标题">¶</a></h2>
<p>看看Django对数据模型改动的支持：<a class="reference internal" href="../topics/migrations.html#module-django.db.migrations" title="django.db.migrations: Schema migration support for Django models"><code class="xref py py-mod docutils literal notranslate"><span class="pre">schema</span> <span class="pre">migrations</span></code></a> （模型迁移）</p>
<p>如果你不介意清除数据，工程目录下的 <code class="docutils literal notranslate"><span class="pre">manage.py</span></code> 工具使用 <a class="reference internal" href="../ref/django-admin.html#django-admin-flush"><code class="xref std std-djadmin docutils literal notranslate"><span class="pre">flush</span></code></a>&nbsp;选项去立即重置数据库到 执行 <a class="reference internal" href="../ref/django-admin.html#django-admin-migrate"><code class="xref std std-djadmin docutils literal notranslate"><span class="pre">migrate</span></code></a> 后的状态</p>
</div>
<div class="section" id="s-do-django-models-support-multiple-column-primary-keys">
<span id="do-django-models-support-multiple-column-primary-keys"></span><h2>Django 支持多列主键吗？<a class="headerlink" href="#do-django-models-support-multiple-column-primary-keys" title="永久链接至标题">¶</a></h2>
<p>不，只支持单列主键。</p>
<p>但这在实践中并不是一个问题，因为无法阻止你添加其他的约束（使用 <code class="docutils literal notranslate"><span class="pre">unique_together</span></code> 模型选项或者在你的数据库中直接创建约束），并在这一级上强化唯一性。管理界面需要单列主键才能工作；例如，你需要唯一值来指定要编辑或删除的对象。</p>
</div>
<div class="section" id="s-does-django-support-nosql-databases">
<span id="does-django-support-nosql-databases"></span><h2>Django 支持 NoSQL 数据库吗？<a class="headerlink" href="#does-django-support-nosql-databases" title="永久链接至标题">¶</a></h2>
<p>Django 官方不支持 NoSQL 数据库，不过有一些项目和拷贝向 Django 中添加了 NoSQL 的功能，如：<a href="#id1"><span class="problematic" id="id2">`Django non-rel`_</span></a> 。</p>
<p>你可以查看 <a class="reference external" href="https://code.djangoproject.com/wiki/NoSqlSupport">the wiki page</a> 来探讨一些其他方案。</p>
</div>
<div class="section" id="s-how-do-i-add-database-specific-options-to-my-create-table-statements-such-as-specifying-myisam-as-the-table-type">
<span id="how-do-i-add-database-specific-options-to-my-create-table-statements-such-as-specifying-myisam-as-the-table-type"></span><h2>如何将特定于数据库的选项添加到我的 CREATE TABLE 语句中，例如将 MylSAM 指定为表类型？<a class="headerlink" href="#how-do-i-add-database-specific-options-to-my-create-table-statements-such-as-specifying-myisam-as-the-table-type" title="永久链接至标题">¶</a></h2>
<p>我们尽量避免在 Django 代码中添加特例来容纳所有特定于数据库的选项，如表数据等。如果您想使用这些选项中的任何一个，请使用：class : <cite>~ django.db.migrations.operations.RunSQL</cite>  操作来创建迁移，包含＂ALTER TABLE＂语句，它们可以执行您想要的操作。</p>
<p>例如，如果你使用 MySQL 并且你想让你的数据表使用 MyISAM 表类型，使用如下 SQL 语句：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">ALTER</span> <span class="n">TABLE</span> <span class="n">myapp_mytable</span> <span class="n">ENGINE</span><span class="o">=</span><span class="n">MyISAM</span><span class="p">;</span>
</pre></div>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">FAQ：数据库和模型</a><ul>
<li><a class="reference internal" href="#how-can-i-see-the-raw-sql-queries-django-is-running">我该如何在Django运行的过程中看到SQL查询？</a></li>
<li><a class="reference internal" href="#can-i-use-django-with-a-pre-existing-database">我可以让 Django 用一个之前已经存在的数据库吗？</a></li>
<li><a class="reference internal" href="#if-i-make-changes-to-a-model-how-do-i-update-the-database">如果我对一个模型做了改动，我该如何去更新数据库？</a></li>
<li><a class="reference internal" href="#do-django-models-support-multiple-column-primary-keys">Django 支持多列主键吗？</a></li>
<li><a class="reference internal" href="#does-django-support-nosql-databases">Django 支持 NoSQL 数据库吗？</a></li>
<li><a class="reference internal" href="#how-do-i-add-database-specific-options-to-my-create-table-statements-such-as-specifying-myisam-as-the-table-type">如何将特定于数据库的选项添加到我的 CREATE TABLE 语句中，例如将 MylSAM 指定为表类型？</a></li>
</ul>
</li>
</ul>

  <h4>上一个主题</h4>
  <p class="topless"><a href="help.html"
                        title="上一章">FAQ：获取帮助</a></p>
  <h4>下一个主题</h4>
  <p class="topless"><a href="admin.html"
                        title="下一章">FAQ：管理</a></p>
  <div role="note" aria-label="source link">
    <h3>本页</h3>
    <ul class="this-page-menu">
      <li><a href="../_sources/faq/models.txt"
            rel="nofollow">显示源代码</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>快速搜索</h3>
    <div class="searchformwrapper">
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="转向" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">12月 07, 2021</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="help.html" title="FAQ：获取帮助">previous</a>
     |
    <a href="index.html" title="Django FAQ" accesskey="U">up</a>
   |
    <a href="admin.html" title="FAQ：管理">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>